package com.splant.android.smart.UILevel.Community;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.Global;
import com.splant.android.smart.ModelLevel.AreaListBean;
import com.splant.android.smart.ModelLevel.WaterListBean;
import com.splant.android.smart.ModelLevel.sPlantRequestParams;
import com.splant.android.smart.SystemDefinedLevel.DeciveManager.DeviveMessages;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.DateTimeSet;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.SlideDateTimeListener;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.DateTimeSelceted.SlideDateTimePicker;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.Dialog.CustomDialog;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.Dialog.LoadingDialog;
import com.splant.android.smart.SystemDefinedLevel.GenericControls.WheelView.WheelView;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Dimen_Util;
import com.splant.android.smart.UILevel.Community.fragments.Garden_AreaList_Adapter;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Garden_AddTimerWaters_Activity extends FragmentActivity implements View.OnClickListener {

    private TextView tv_startTime;
    private TextView tv_waterLength;
    private TextView tv_month1;
    private TextView tv_month2;
    private TextView tv_month3;
    private TextView tv_month4;
    private TextView tv_month5;
    private TextView tv_month6;
    private TextView tv_month7;
    private TextView tv_month8;
    private TextView tv_month9;
    private TextView tv_month10;
    private TextView tv_month11;
    private TextView tv_month12;
    private TextView tv_month13;
    private TextView tv_month14;
    private TextView tv_month15;
    private TextView tv_month16;
    private TextView tv_month17;
    private TextView tv_month18;
    private TextView tv_month19;
    private TextView tv_month20;
    private TextView tv_month21;
    private TextView tv_month22;
    private TextView tv_month23;
    private TextView tv_month24;
    private TextView tv_month25;
    private TextView tv_month26;
    private TextView tv_month27;
    private TextView tv_month28;
    private TextView tv_month29;
    private TextView tv_month30;
    private TextView tv_month31;

    private TextView tv_areaName;
    private int waterLength = 10;
    private Button btn_save;
    private Button btn_goBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.garden_addtimerwaters_activity);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        btn_save = (Button) findViewById(R.id.btn_save);
        tv_startTime = (TextView) findViewById(R.id.tv_startTime);
        tv_waterLength = (TextView) findViewById(R.id.tv_waterLength);
        tv_areaName = (TextView) findViewById(R.id.tv_areaName);
        lrv_waterList = (LRecyclerView) findViewById(R.id.lrv_waterList);

        tv_month1 = (TextView) findViewById(R.id.tv_month1);
        tv_month2 = (TextView) findViewById(R.id.tv_month2);
        tv_month3 = (TextView) findViewById(R.id.tv_month3);
        tv_month4 = (TextView) findViewById(R.id.tv_month4);
        tv_month5 = (TextView) findViewById(R.id.tv_month5);
        tv_month6 = (TextView) findViewById(R.id.tv_month6);
        tv_month7 = (TextView) findViewById(R.id.tv_month7);
        tv_month8 = (TextView) findViewById(R.id.tv_month8);
        tv_month9 = (TextView) findViewById(R.id.tv_month9);
        tv_month10 = (TextView) findViewById(R.id.tv_month10);
        tv_month11 = (TextView) findViewById(R.id.tv_month11);
        tv_month12 = (TextView) findViewById(R.id.tv_month12);
        tv_month13 = (TextView) findViewById(R.id.tv_month13);
        tv_month14 = (TextView) findViewById(R.id.tv_month14);
        tv_month15 = (TextView) findViewById(R.id.tv_month15);
        tv_month16 = (TextView) findViewById(R.id.tv_month16);
        tv_month17 = (TextView) findViewById(R.id.tv_month17);
        tv_month18 = (TextView) findViewById(R.id.tv_month18);
        tv_month19 = (TextView) findViewById(R.id.tv_month19);
        tv_month20 = (TextView) findViewById(R.id.tv_month20);
        tv_month21 = (TextView) findViewById(R.id.tv_month21);
        tv_month22 = (TextView) findViewById(R.id.tv_month22);
        tv_month23 = (TextView) findViewById(R.id.tv_month23);
        tv_month24 = (TextView) findViewById(R.id.tv_month24);
        tv_month25 = (TextView) findViewById(R.id.tv_month25);
        tv_month26 = (TextView) findViewById(R.id.tv_month26);
        tv_month27 = (TextView) findViewById(R.id.tv_month27);
        tv_month28 = (TextView) findViewById(R.id.tv_month28);
        tv_month29 = (TextView) findViewById(R.id.tv_month29);
        tv_month30 = (TextView) findViewById(R.id.tv_month30);
        tv_month31 = (TextView) findViewById(R.id.tv_month31);

        init();

        btn_goBack.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        tv_startTime.setOnClickListener(this);
        tv_waterLength.setOnClickListener(this);
        tv_areaName.setOnClickListener(this);

        tv_month1.setOnClickListener(this);
        tv_month2.setOnClickListener(this);
        tv_month3.setOnClickListener(this);
        tv_month4.setOnClickListener(this);
        tv_month5.setOnClickListener(this);
        tv_month6.setOnClickListener(this);
        tv_month7.setOnClickListener(this);
        tv_month8.setOnClickListener(this);
        tv_month9.setOnClickListener(this);
        tv_month10.setOnClickListener(this);
        tv_month11.setOnClickListener(this);
        tv_month12.setOnClickListener(this);
        tv_month13.setOnClickListener(this);
        tv_month14.setOnClickListener(this);
        tv_month15.setOnClickListener(this);
        tv_month16.setOnClickListener(this);
        tv_month17.setOnClickListener(this);
        tv_month18.setOnClickListener(this);
        tv_month19.setOnClickListener(this);
        tv_month20.setOnClickListener(this);
        tv_month21.setOnClickListener(this);
        tv_month22.setOnClickListener(this);
        tv_month23.setOnClickListener(this);
        tv_month24.setOnClickListener(this);
        tv_month25.setOnClickListener(this);
        tv_month26.setOnClickListener(this);
        tv_month27.setOnClickListener(this);
        tv_month28.setOnClickListener(this);
        tv_month29.setOnClickListener(this);
        tv_month30.setOnClickListener(this);
        tv_month31.setOnClickListener(this);

    }

    private sPlantAsyncHttpClient sPlantClient;

    private void init() {

        sPlantClient = sPlantAsyncHttpClient.getInstance();

        garden_waterList_select_adapter = new Garden_WaterList_Select_Adapter(this);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(garden_waterList_select_adapter);

        lrv_waterList.setHasFixedSize(true);
        lrv_waterList.setLayoutManager(new GridLayoutManager(this, 1));
        lrv_waterList.setAdapter(lRecyclerViewAdapter);
        lrv_waterList.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);

        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setVertical(R.dimen.gaiaa_devicelist_dh)
                .setColorResource(R.color.garden_color0)
                .build();

        lrv_waterList.addItemDecoration(divider);

        //   lrv_waterList.setFooterViewColor(R.color.colorAccent, R.color.garden_color2, android.R.color.white);
        lrv_waterList.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        lrv_waterList.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        lrv_waterList.setOnRefreshListener(onRefreshListener);

        lrv_waterList.setOnLoadMoreListener(onLoadMoreListener);

        getWaterList();
    }

    private static final int REQUEST_COUNT = 8;
    private String areaId = "";
    private LRecyclerView lrv_waterList;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private Garden_WaterList_Select_Adapter garden_waterList_select_adapter;
    private List<WaterListBean.Water> waterList = new ArrayList<>();

    private void getWaterList() {

        RequestParams params = new RequestParams();
        params.put("userId", Global.user.getUserId());

        if (areaId.equals("") == false) {
            params.put("areaId", areaId);
            params.put("pageNo", pageNo);
            params.put("pageSize", REQUEST_COUNT);
        } else {
            params.put("pageNo", pageNo);
            params.put("pageSize", REQUEST_COUNT);
            lrv_waterList.setLoadMoreEnabled(true);
        }

        lrv_waterList.setNoMore(false);
        sPlantClient.get(sPlantAsyncHttpClient.GETWATERDEVICES, params, new sPlantJsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    if (response.getInt("succeed") == 1) {
                        Gson gson = new Gson();
                        WaterListBean waterListBean = gson.fromJson(response.toString(), WaterListBean.class);

                        if (waterListBean.getWaterList() != null)
                            waterList.addAll(waterListBean.getWaterList());

                        garden_waterList_select_adapter.setDataList(waterList);

                        if (waterListBean.getWaterList() == null || waterListBean.getWaterList().size() < REQUEST_COUNT)
                        {
                            lrv_waterList.setNoMore(true);
                          //  lrv_waterList.setLoadMoreEnabled(false);
                        }


                        lrv_waterList.refreshComplete(REQUEST_COUNT);

                    }
                } catch (Exception e) {

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                Toast.makeText(Garden_AddTimerWaters_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private List<AreaListBean.Area> areaList = new ArrayList<>();

    private void showAreaList() {

        View popupView = getLayoutInflater().inflate(R.layout.garden_arealist_popupwindow, null);

        final ListView lv_areaList = (ListView) popupView.findViewById(R.id.lv_areaList);

        final PopupWindow window = new PopupWindow(popupView, System_Dimen_Util.dip2px(this, 200), System_Dimen_Util.dip2px(this, 400));

        window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));

        window.setFocusable(true);

        window.setOutsideTouchable(true);

        window.showAsDropDown(tv_areaName, 0, 2);

        RequestParams params = new RequestParams();
        params.put("userId", Global.user.getUserId());

        lv_areaList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                window.dismiss();

                areaId = areaList.get(position).getAreaId();

                tv_areaName.setText(areaList.get(position).getAreaName());

                pageNo = 0;
                waterList.clear();

                getWaterList();

            }
        });

        sPlantClient.get(sPlantAsyncHttpClient.GETAREALIST, params, new sPlantJsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    if (response.getInt("succeed") == 1) {

                        Gson gson = new Gson();

                        AreaListBean areaListBean = gson.fromJson(response.toString(), AreaListBean.class);

                        if (areaListBean.getAreaList() != null && areaListBean.getAreaList().size() > 0) {

                            areaList.clear();

                            AreaListBean.Area areaAll = new AreaListBean.Area();
                            areaAll.setAreaId("");
                            areaAll.setAreaName("全部区域");

                            areaList.add(areaAll);
                            areaList.addAll(areaListBean.getAreaList());

                            Garden_AreaList_Adapter garden_areaList_adapter = new Garden_AreaList_Adapter(Garden_AddTimerWaters_Activity.this, areaList);

                            lv_areaList.setAdapter(garden_areaList_adapter);

                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                Toast.makeText(Garden_AddTimerWaters_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();

            }
        });

    }

    private AlertDialog waterTimeDialog;

    private List<String> getTimeData() {

        for (int i = 1; i <= 10; i++) {
            mDatas.add(i + " 分钟");
        }

        for (int i = 15; i <= 30; i += 5) {
            mDatas.add(i + " 分钟");
        }

        for (int i = 40; i <= 60; i += 10) {
            mDatas.add(i + " 分钟");
        }

        return mDatas;
    }

    private List<String> mDatas = new ArrayList<>();

    private void selectWaterLength() {
        View outerView = LayoutInflater.from(this).inflate(R.layout.system_unit_wheel_view, null);
        WheelView wv = (WheelView) outerView.findViewById(R.id.wheel_view_wv);
        wv.setOffset(2);
        wv.setItems(getTimeData());
        wv.setSeletion(9);

        wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                String[] items = item.split(" ");
                waterLength = Integer.parseInt(items[0]);

            }
        });
        TextView tv = new TextView(this);
        tv.setText("请选择");
        tv.setTextSize(20);
        tv.setPadding(10, 10, 10, 10);
        tv.setGravity(Gravity.CENTER);
        tv.setTextColor(Color.parseColor("#ffaa66cc"));

        waterTimeDialog = new AlertDialog.Builder(this)
                .setCustomTitle(tv)
                .setView(outerView)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        tv_waterLength.setText(waterLength + "分钟");

                    }
                }).create();
        waterTimeDialog.show();

        WindowManager.LayoutParams params = waterTimeDialog.getWindow().getAttributes();
        params.width = (2 * DeviveMessages.getScreenInfo(this, getWindowManager()).getScreenWidth()) / 5;
        waterTimeDialog.getWindow().setAttributes(params);
    }

    private void selectWaterStartTime() {
        try {
            DateTimeSet.dateTimeType = DateTimeSet.DateTimeType.TIME;
            new SlideDateTimePicker.Builder(getSupportFragmentManager())
                    .setListener(slideDateTimeListener)
                    .setInitialDate(new Date())
                    //.setMinDate(minDate)
                    //.setMaxDate(maxDate)
                    //.setIs24HourTime(true)
                    .setTheme(SlideDateTimePicker.HOLO_LIGHT)
                    // .setIndicatorColor(R.color.system_appBg)
                    .build()
                    .show();
        } catch (Exception e) {

        }
    }

    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

    private String selectStartTime = "12:00";
    private SlideDateTimeListener slideDateTimeListener = new SlideDateTimeListener() {

        @Override
        public void onDateTimeSet(Date date) {

            selectStartTime = sdf.format(date);

            tv_startTime.setText(selectStartTime);

        }

        @Override
        public void onDateTimeCancel() {

        }
    };

    private void check() {
        if (timerCycle.equals("")) {
            Toast.makeText(Garden_AddTimerWaters_Activity.this, "请选择给水周期！", Toast.LENGTH_SHORT).show();
            return;
        }

        List<WaterListBean.Water> waterList = garden_waterList_select_adapter.getDataList();

        if (waterList != null) {
            boolean tf = false;
            for (WaterListBean.Water water : waterList) {
                if (water.getSelectedSwitchs().equals("") == false && water.getTimerWaterList() != null)
                    tf = true;
            }
            if (tf) {
                final CustomDialog.Builder builder = new CustomDialog.Builder(this);
                builder.setTitle("提示信息");
                builder.setMessage("此条策略涉及的开关已存在定时给水策略，确认需要增加配置？");
                builder.setPositiveButton(getString(R.string.system_ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        save();
                    }
                });
                builder.setNegativeButton(getString(R.string.system_cancel), new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        }


    }

    private void save() {


        List<WaterListBean.Water> waterList = garden_waterList_select_adapter.getDataList();

        JSONArray timerWaterList = new JSONArray();
        sPlantRequestParams params = new sPlantRequestParams();
        try {
            for (WaterListBean.Water water : waterList) {
                if (water.getSelectedSwitchs().contains("1")) {
                    JSONObject timerWater = new JSONObject();
                    timerWater.put("waterId", water.getWaterId());
                    timerWater.put("waterName", water.getWaterName());
                    timerWater.put("switchNo", 1);
                    timerWater.put("waterCycle", timerCycle);
                    timerWater.put("startTime", selectStartTime);
                    timerWater.put("waterLength", waterLength);
                    timerWater.put("allowUsed", true);
                    timerWaterList.put(timerWater);
                }
                if (water.getSelectedSwitchs().contains("2")) {
                    JSONObject timerWater = new JSONObject();
                    timerWater.put("waterId", water.getWaterId());
                    timerWater.put("waterName", water.getWaterName());
                    timerWater.put("switchNo", 2);
                    timerWater.put("waterCycle", timerCycle);
                    timerWater.put("startTime", selectStartTime);
                    timerWater.put("waterLength", waterLength);
                    timerWater.put("allowUsed", true);
                    timerWaterList.put(timerWater);

                }
                if (water.getSelectedSwitchs().contains("3")) {
                    JSONObject timerWater = new JSONObject();
                    timerWater.put("waterId", water.getWaterId());
                    timerWater.put("waterName", water.getWaterName());
                    timerWater.put("switchNo", 3);
                    timerWater.put("waterCycle", timerCycle);
                    timerWater.put("startTime", selectStartTime);
                    timerWater.put("waterLength", waterLength);
                    timerWater.put("allowUsed", true);
                    timerWaterList.put(timerWater);
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (timerWaterList.length() == 0) {
            Toast.makeText(Garden_AddTimerWaters_Activity.this, "请选择浇灌设备！", Toast.LENGTH_SHORT).show();
            return;
        }
        params.putParams("timerWaters", timerWaterList);

        sPlantClient.post(this, sPlantAsyncHttpClient.ADD_TIMERWATER, params, new sPlantJsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        try {
                            if (response.getInt("succeed") == 1) {

                                Toast.makeText(Garden_AddTimerWaters_Activity.this, "新建成功！", Toast.LENGTH_SHORT).show();

                            } else if (response.getInt("succeed") == 0) {

                                Toast.makeText(Garden_AddTimerWaters_Activity.this, "新建失败！", Toast.LENGTH_SHORT).show();

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                        Toast.makeText(Garden_AddTimerWaters_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();

                    }
                }
        );

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_goBack:
                finish();
                break;
            case R.id.btn_save:
                check();
                break;
            case R.id.tv_startTime:
                selectWaterStartTime();
                break;
            case R.id.tv_waterLength:
                selectWaterLength();
                break;
            case R.id.tv_areaName:
                showAreaList();
                break;
            case R.id.tv_month1:
                selectCancelDay(1, tv_month1);
                break;
            case R.id.tv_month2:
                selectCancelDay(2, tv_month2);
                break;
            case R.id.tv_month3:
                selectCancelDay(3, tv_month3);
                break;
            case R.id.tv_month4:
                selectCancelDay(4, tv_month4);
                break;
            case R.id.tv_month5:
                selectCancelDay(5, tv_month5);
                break;
            case R.id.tv_month6:
                selectCancelDay(6, tv_month6);
                break;
            case R.id.tv_month7:
                selectCancelDay(7, tv_month7);
                break;
            case R.id.tv_month8:
                selectCancelDay(8, tv_month8);
                break;
            case R.id.tv_month9:
                selectCancelDay(9, tv_month9);
                break;
            case R.id.tv_month10:
                selectCancelDay(10, tv_month10);
                break;
            case R.id.tv_month11:
                selectCancelDay(11, tv_month11);
                break;
            case R.id.tv_month12:
                selectCancelDay(12, tv_month12);
                break;
            case R.id.tv_month13:
                selectCancelDay(13, tv_month13);
                break;
            case R.id.tv_month14:
                selectCancelDay(14, tv_month14);
                break;
            case R.id.tv_month15:
                selectCancelDay(15, tv_month15);
                break;
            case R.id.tv_month16:
                selectCancelDay(16, tv_month16);
                break;
            case R.id.tv_month17:
                selectCancelDay(17, tv_month17);
                break;
            case R.id.tv_month18:
                selectCancelDay(18, tv_month18);
                break;
            case R.id.tv_month19:
                selectCancelDay(19, tv_month19);
                break;
            case R.id.tv_month20:
                selectCancelDay(20, tv_month20);
                break;
            case R.id.tv_month21:
                selectCancelDay(21, tv_month21);
                break;
            case R.id.tv_month22:
                selectCancelDay(22, tv_month22);
                break;
            case R.id.tv_month23:
                selectCancelDay(23, tv_month23);
                break;
            case R.id.tv_month24:
                selectCancelDay(24, tv_month24);
                break;
            case R.id.tv_month25:
                selectCancelDay(25, tv_month25);
                break;
            case R.id.tv_month26:
                selectCancelDay(26, tv_month26);
                break;
            case R.id.tv_month27:
                selectCancelDay(27, tv_month27);
                break;
            case R.id.tv_month28:
                selectCancelDay(28, tv_month28);
                break;
            case R.id.tv_month29:
                selectCancelDay(29, tv_month29);
                break;
            case R.id.tv_month30:
                selectCancelDay(30, tv_month30);
                break;
            case R.id.tv_month31:
                selectCancelDay(31, tv_month31);
                break;
        }
    }

    private String timerCycle = "";

    public void selectCancelDay(int day, TextView tv_month) {

        String dayStr = String.valueOf(day);

        if (timerCycle.contains(",")) {
            String[] timerCycleAry = timerCycle.split(",");
            boolean tf = false;
            timerCycle = "";
            for (String d : timerCycleAry) {
                if (d.equals(dayStr))
                    tf = true;
                else
                    timerCycle += d + ",";
            }
            if (tf)
                tv_month.setBackgroundResource(R.color.garden_monthtable_bg_normal);
            else {
                timerCycle += dayStr + ",";
                tv_month.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
            }
            timerCycle = timerCycle.substring(0, timerCycle.length() - 1);
        } else if (timerCycle.length() == 1) {
            if (timerCycle.equals(String.valueOf(day))) {
                timerCycle = "";
                tv_month.setBackgroundResource(R.color.garden_monthtable_bg_normal);
            } else {
                timerCycle += "," + day;
                tv_month.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
            }
        } else {
            timerCycle = String.valueOf(day);
            tv_month.setBackgroundResource(R.color.garden_monthtable_bg_unselected);
        }
    }

    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            pageNo = 0;
            waterList.clear();
            getWaterList();
        }
    };
    private int pageNo = 0;
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            pageNo += 1;
            getWaterList();
        }
    };
}
